/*
 * socketselect.h
 *
 *  Created on: 2012-7-10
 *      Author: epmm
 */

#ifndef SOCKETSELECT_H_
#define SOCKETSELECT_H_

class CSocketSelect: public CSocketBase
{
	fd_set m_readset;
public:
	typedef struct tagsocketinfo
	{
		int m_socket;
		char m_addr[17];
		int m_port;
	} SOCKETINFO, *PSOCKETINFO;

	typedef vector<SOCKETINFO> SOCK_ARRAY;

	class IOnSocket
	{
	public:
		virtual ~IOnSocket()
		{

		}
		virtual int OnRecv(const string& strBuffer, const int sock_fd) = 0;
		virtual int OnSecv(const string& strBuffer, const int sock_fd) = 0;
	};
	IOnSocket* m_OnSocket;
	SOCK_ARRAY m_ArySocket;

public:
	CSocketSelect();
	virtual ~CSocketSelect();

public:
	int InitServer(sockaddr* addr,int addrlen,int domain = AF_INET, int type = SOCK_STREAM,int protocol = 0);
	int InitSelectSvr();
	int startUp();
	int setOnSocket(IOnSocket* pSocket);
	const char* GetHostIP(int socket);
private:
	int Select();
	int Accept();
	int CreateSocketInfo(int socket);
	int FreeSocketInfo(int socket);

	int Recv(int nClientSocket, string& strBuffer, int nFlag = 0);
protected:
	virtual int OnRecv(const string strBuffer, const int sock_fd);
	virtual int OnSecv(const string strBuffer, const int sock_fd);

};

#endif /* SOCKETSELECT_H_ */
